package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysExpertSelect;
import com.ruoyi.system.service.ISysExpertSelectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProjectExpertPrior;
import com.ruoyi.system.service.ISysProjectExpertPriorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优先专家Controller
 * 
 * @author ruoyi
 * @date 2024-11-02
 */
@RestController
@RequestMapping("/system/projectexpertprior")
public class SysProjectExpertPriorController extends BaseController
{
    @Autowired
    private ISysProjectExpertPriorService sysProjectExpertPriorService;

    @Autowired
    private ISysExpertSelectService sysExpertSelectService;

    /**
     * 查询优先专家列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertprior:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProjectExpertPrior sysProjectExpertPrior)
    {
        startPage();
        List<SysProjectExpertPrior> list = sysProjectExpertPriorService.selectSysProjectExpertPriorList(sysProjectExpertPrior);
        return getDataTable(list);
    }

    /**
     * 导出优先专家列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertprior:export')")
    @Log(title = "优先专家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProjectExpertPrior sysProjectExpertPrior)
    {
        List<SysProjectExpertPrior> list = sysProjectExpertPriorService.selectSysProjectExpertPriorList(sysProjectExpertPrior);
        ExcelUtil<SysProjectExpertPrior> util = new ExcelUtil<SysProjectExpertPrior>(SysProjectExpertPrior.class);
        util.exportExcel(response, list, "优先专家数据");
    }

    /**
     * 获取优先专家详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertprior:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysProjectExpertPriorService.selectSysProjectExpertPriorById(id));
    }

    /**
     * 新增优先专家
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertprior:add')")
    @Log(title = "优先专家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProjectExpertPrior sysProjectExpertPrior)
    {
        int result = sysProjectExpertPriorService.insertSysProjectExpertPrior(sysProjectExpertPrior);
        // 查询数量更新到 sys_expert_select
        SysProjectExpertPrior data = new SysProjectExpertPrior();
        data.setExpertSelectId(sysProjectExpertPrior.getExpertSelectId());
        List<SysProjectExpertPrior> projectExpertPrior =  sysProjectExpertPriorService.selectSysProjectExpertPriorList(data);

        Long expertSelectId = sysProjectExpertPrior.getExpertSelectId();
        SysExpertSelect expertSelect = sysExpertSelectService.selectSysExpertSelectById(expertSelectId);
        if (expertSelect != null){
            expertSelect.setExpertPriorNum(projectExpertPrior.size());
            sysExpertSelectService.updateSysExpertSelect(expertSelect);
        }
        return toAjax(result);
    }

    /**
     * 修改优先专家
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertprior:edit')")
    @Log(title = "优先专家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProjectExpertPrior sysProjectExpertPrior)
    {
        return toAjax(sysProjectExpertPriorService.updateSysProjectExpertPrior(sysProjectExpertPrior));
    }

    /**
     * 删除优先专家
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertprior:remove')")
    @Log(title = "优先专家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}/{selectid}")
    public AjaxResult remove(@PathVariable Long[] ids, @PathVariable Long selectid)
    {
        int result = sysProjectExpertPriorService.deleteSysProjectExpertPriorByIds(ids);
        // 查询数量更新到 sys_expert_select
        SysProjectExpertPrior data = new SysProjectExpertPrior();
        data.setExpertSelectId(selectid);
        List<SysProjectExpertPrior> projectExpertPrior =  sysProjectExpertPriorService.selectSysProjectExpertPriorList(data);
        SysExpertSelect expertSelect = sysExpertSelectService.selectSysExpertSelectById(selectid);
        System.out.println(expertSelect);
        System.out.println(projectExpertPrior);
        if (expertSelect != null){
            expertSelect.setExpertPriorNum(projectExpertPrior.size());
            sysExpertSelectService.updateSysExpertSelect(expertSelect);
        }
        return toAjax(result);
    }
}
